package com.qiudaozhang.jblog.article.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiudaozhang.jblog.article.mapper.TagMapper;
import com.qiudaozhang.jblog.article.service.ArticleTagService;
import com.qiudaozhang.jblog.article.service.TagService;
import com.qiudaozhang.jblog.model.Tag;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author 邱道长
 * 时间： 2018/12/21 11:56
 */

@Service(version = "${service.version}",group = "${dubbo.group}")
public class TagServiceImpl implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private ArticleTagService articleTagSerivce;
    @Override
    public PageInfo<Tag> queryPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Tag> l = tagMapper.queryAll();
        PageInfo<Tag> pageInfo = new PageInfo<>(l);
        return pageInfo;
    }

    @Override
    public void deleteById(Integer tagId) {
        // 删除标签，将标签与博客的关联表的关系进行清除
        articleTagSerivce.deleteByTagId(tagId);
        tagMapper.deleteByTagId(tagId);
    }

    @Override
    public void update(Tag tag) {
        tag.setUpdateTime(new Date());
        tagMapper.update(tag);
    }

    @Override
    public PageInfo<Tag> queryPageParam(int pageNum, int pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        Tag tag = new Tag();
        tag.setName(name);
        List<Tag> l = tagMapper.query(tag);
        PageInfo<Tag> pageInfo = new PageInfo<>(l);
        return pageInfo;
    }

    @Override
    public void save(Tag tag) {
        tagMapper.insert(tag);
    }

    @Override
    public List<Tag> selectAll() {
        return tagMapper.queryAll();
    }
}
